using System;
using System.Drawing;
using System.Collections.Generic;
using System.Text;

using TTDotNet.Util;
using TTDotNet.Engine.Vehicles;
using TTDotNet.Engine.ThreeDEngine;
using TTDotNet.Engine.GUI.Buttons;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace TTDotNet.Engine.GUI.Windows
{
    public class VehicleWindow : Window
    {
        AJCamera vehcCam;
        Vehicle vehicle;

        private int statusBar;

        public VehicleWindow(Vehicle vehc, PointF screenLocation)
        {
            this.vehicle = vehc;
            
            int cSize = 24;
            int border = 2;
            int colWidth = 38;
            
            MinimumSize = new SizeF(colWidth + 400, (2 * cSize) + (6 * colWidth) + (9 * border));

            float width = MinimumSize.Width + 1;
            float height = MinimumSize.Height + 1;

            SetScreenRectangle(new RectangleF(screenLocation, new SizeF(width, height)));

            vehcCam = new AJCamera(width - (3 * border) - colWidth, height - (4 * border) - (cSize * 2), true);
            vehcCam.Position = new Vector3(3f, 0.5f, 3f);
            vehcCam.Update();
            vehcCam.FarClipPlane = 100;

            int controlNum = 0;

            windowControls[controlNum++] = new Label(new RectangleF(new PointF(0, 0), new SizeF(width, height)), "", 1, Color.Black, "Arial",
                                                        Game.textureCacher.LoadTexture("artwork/colours/black.jpg"), DrawTextFormat.Center | DrawTextFormat.VerticalCenter);

            windowControls[controlNum++] = new CloseButton(new RectangleF(new PointF(border, border), new SizeF(cSize, cSize)),
                                            Game.textureCacher.LoadTexture("artwork/128/close.jpg"), Game.textureCacher.LoadTexture("artwork/128/closeDown.jpg"), this);

            windowControls[controlNum++] = new TitleBar(new RectangleF(new PointF(cSize + (2 * border), border), new SizeF(width - ((2 * cSize) + (4 * border)), cSize)),
                                            vehc.Name, 15, Color.White, "Arial", Game.textureCacher.LoadTexture("artwork/colours/ttdOrange.jpg"), this);

            windowControls[controlNum++] = new GameButton(new RectangleF(new PointF(width - border - cSize, border), new SizeF(cSize, cSize)), "", 1, Color.Black,
                                                            "Arial", Game.textureCacher.LoadTexture("artwork/128/pinUp.jpg"), Game.textureCacher.LoadTexture("artwork/128/pinDown.jpg"));

            windowControls[controlNum++] = new ViewWindow(new RectangleF(new PointF(border, cSize + (border * 2)), new SizeF(width - (3 * border) - colWidth, height - (4 * border) - (cSize * 2))),
                                                                vehcCam);

            windowControls[controlNum++] = new GameButton(new RectangleF(new PointF(width - border - colWidth, cSize + (2 * border)), new SizeF(colWidth, colWidth)),
                                                            "", 1, Color.Black, "Arial", Game.textureCacher.LoadTexture("artwork/128/human_eye.jpg"),
                                                            Game.textureCacher.LoadTexture("artwork/128/human_eye_down.jpg"));

            windowControls[controlNum++] = new GameButton(new RectangleF(new PointF(width - border - colWidth, cSize + (3 * border) + colWidth), new SizeF(colWidth, colWidth)),
                                                            "", 1, Color.Black, "Arial", Game.textureCacher.LoadTexture("artwork/128/bus_depot.jpg"),
                                                            Game.textureCacher.LoadTexture("artwork/128/bus_depot_down.jpg"));

            windowControls[controlNum++] = new GameButton(new RectangleF(new PointF(width - border - colWidth, cSize + (4 * border) + (colWidth * 2)), new SizeF(colWidth, colWidth)),
                                                            "", 1, Color.Black, "Arial", Game.textureCacher.LoadTexture("artwork/128/traffic_lights_r.jpg"),
                                                            Game.textureCacher.LoadTexture("artwork/128/traffic_lights_g.jpg"));

            windowControls[controlNum++] = new GameButton(new RectangleF(new PointF(width - border - colWidth, cSize + (5 * border) + (colWidth * 3)), new SizeF(colWidth, colWidth)),
                                                            "", 1, Color.Black, "Arial", Game.textureCacher.LoadTexture("artwork/128/reverse.jpg"),
                                                            Game.textureCacher.LoadTexture("artwork/128/reverse_down.jpg"));

            windowControls[controlNum++] = new GameButton(new RectangleF(new PointF(width - border - colWidth, cSize + (6 * border) + (colWidth * 4)), new SizeF(colWidth, colWidth)),
                                                            "", 1, Color.Black, "Arial", Game.textureCacher.LoadTexture("artwork/128/orders.jpg"),
                                                            Game.textureCacher.LoadTexture("artwork/128/orders_down.jpg"));
            windowControls[controlNum - 1].OnControl += new Control.ControlDelegate(CreateVehicleOrdersWindow);

            windowControls[controlNum++] = new GameButton(new RectangleF(new PointF(width - border - colWidth, cSize + (7 * border) + (colWidth * 5)), new SizeF(colWidth, colWidth)),
                                                            "", 1, Color.Black, "Arial", Game.textureCacher.LoadTexture("artwork/128/vehicle_details.jpg"),
                                                            Game.textureCacher.LoadTexture("artwork/128/vehicle_details_down.jpg"));

            windowControls[controlNum++] = new GameButton(new RectangleF(new PointF(border, height - border - cSize), new SizeF(width - (3 * border) - cSize, cSize)),
                                                            "No Orders, 0mph", 14, Color.LightBlue, "Arial", Game.textureCacher.LoadTexture("artwork/colours/grey.jpg"),
                                                            Game.textureCacher.LoadTexture("artwork/colours/silver.jpg"));
            windowControls[controlNum - 1].OnControl += new Control.ControlDelegate(StatusBarClicked);
            statusBar = controlNum - 1;

            windowControls[controlNum++] = new ResizeButton(new RectangleF(new PointF(width - cSize - border, height - border - cSize), new SizeF(cSize, cSize)),
                                                            Game.textureCacher.LoadTexture("artwork/128/resize_handle.jpg"),
                                                            Game.textureCacher.LoadTexture("artwork/128/resize_handle_down.jpg"), this);
        }

        private void CreateVehicleOrdersWindow(object data, object tag)
        {
            OrdersWindow window = new OrdersWindow(vehicle, new PointF(700, 700));
            Game.newGUI.OpenNewWindow(window);
        }

        private void StatusBarClicked(object data, object tag)
        {
            vehicle.IsStopped = !vehicle.IsStopped;
        }

        public override void Update()
        {
            vehcCam.Position = new Vector3(vehicle.Location.X - 1.5f, 1.5f + vehicle.Location.Y, vehicle.Location.Z - 1.5f);
            vehcCam.LookAt = new Vector3(vehicle.Location.X, 0.0f + vehicle.Location.Y, vehicle.Location.Z);
            vehcCam.Update();

            windowControls[statusBar].Text = vehicle.StatusText;
        }

        public AJCamera Camera
        {
            get { return vehcCam; }
        }

    }
}
